package com.mc.moring.widge;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.mc.moring.R;

/**
 * @ProjectName: ZhiLiProject
 * @Package: com.xx.text.widget
 * @ClassName: DialogView
 * @Author: ANWEN
 * @CreateDate: 12/12/23 7:52 PM
 * @UpdateUser:
 * @UpdateDate: 12/12/23 7:52 PM
 * @UpdateRemark:
 * @Version:
 */
public class PersonalInfoComfirmDialog extends Dialog {

    DialogViewOnClickListener dialogViewOnClickListener;

    private String runs;

    public PersonalInfoComfirmDialog(@NonNull Context context, String runs) {
        super(context, R.style.LoadDialogStyle1);
        this.runs = runs;
    }

    public interface DialogViewOnClickListener {
        void onClickConfirm();

        void onClickCancel();
    }

    public void setDialogViewOnClickListener(DialogViewOnClickListener dialogViewOnClickListener) {
        this.dialogViewOnClickListener = dialogViewOnClickListener;
    }

    public void setMagin(Activity activity, View layoutParent, int number) {
        if (layoutParent.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        } else if (layoutParent.getLayoutParams() instanceof FrameLayout.LayoutParams) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) layoutParent.getLayoutParams();
            lp.width = getScreenWidth(activity) - number;
            layoutParent.setLayoutParams(lp);
        }
    }

    public static int getScreenWidth(Activity context) {
        Display display = context.getWindowManager().getDefaultDisplay();    //获得默认（本地）的显示设备
        int screen_width = display.getWidth();
        return screen_width;
    }

    private boolean cancel;

    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        this.cancel = cancel;
        super.setCanceledOnTouchOutside(cancel);
    }

    @Override
    public void onBackPressed() {
        if (!cancel) {
            return;
        }
        super.onBackPressed();
    }

    public void showToPermissionSettingDialog(Context context) {
        View view = View.inflate(context, R.layout.dialog_personal_info_confirm, null);
        Window window = this.getWindow();
        window.setBackgroundDrawableResource(R.color.transparent);
        TextView textview_text = view.findViewById(R.id.textview_text);
        TextView tv_discrepar = view.findViewById(R.id.tv_discrepar);
        TextView tv_confirmar = view.findViewById(R.id.tv_confirmar);
        FrameLayout frameLayout = view.findViewById(R.id.layoutParent);
        TextView tv_runs = view.findViewById(R.id.tv_runs);
        tv_runs.setText(runs);
        PersonalInfoComfirmDialog myDialog = new PersonalInfoComfirmDialog(context, runs);
        myDialog.setMagin((Activity) context, frameLayout, 56 * 2);
        myDialog.setContentView(view);
        myDialog.show();

        tv_discrepar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogViewOnClickListener != null) {
                    dialogViewOnClickListener.onClickCancel();
                }
                myDialog.dismiss();
            }
        });
        tv_confirmar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogViewOnClickListener != null) {
                    dialogViewOnClickListener.onClickConfirm();
                }
                myDialog.dismiss();
            }
        });
    }
}
